<?php
namespace app\service;
use alipay\AlipayEbppIndustryCommunityDeliveryrecentorderDetect;
use alipay\AopClient;
use alipay\AlipayOpenAuthTokenAppRequest;    //查询物业token
use alipay\AlipaySystemOauthTokenRequest;//换取授权访问令牌
use alipay\AlipayTradeCreateRequest;//生成订单
use alipay\AlipayTradeCloseRequest;
use alipay\AlipayOpenAppMiniTemplatemessageSendRequest;//小程序模板信息
use alipay\AlipayOpenAppQrcodeCreateRequest;//创建二维码
use alipay\ZolozIdentificationCustomerCertifyzhubQueryRequest;
use alipay\ZolozIdentificationUserWebQueryRequest;
use alipay\AlipayEbppCommunityDeliveryidentityDetectRequest;
class apixcxService{

//    const PRIKEYPATH = '/www/wwwroot/alizaozao/public/rsa/hzymt/prikey.txt';//私钥路径
//    const PUBKEYPATH = '/www/wwwroot/alizaozao/public/rsa/hzymt/alipay_pubkey.txt';//支付宝公钥路径
    const PRIKEYPATH = '/www/wwwroot/alizaozao/public/rsa/muyao/prikey.txt';//私钥路径
    const PUBKEYPATH = '/www/wwwroot/alizaozao/public/rsa/muyao/alipay_pubkey.txt';//支付宝公钥路径
     //XIOA
    public function xcxgetPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2021006100621758';//2019061665593126
        $aop->rsaPrivateKey = file_get_contents(self::PRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::PUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }
    //
    public function test($data){
        $data = json_decode($data);
        $aop = $this->xcxgetPublicContent();
        $request = new AlipayEbppCommunityDeliveryidentityDetectRequest ();
        $request->setBizContent("{" .
            "\"delivery_user_id\":\"$data->delivery_user_id\"," .
            "\"community_id\":\"$data->community_id\"," .
            "\"company_code\":\"$data->company_code\"," .
            "\"company_name\":\"$data->company_name\"," .
            "\"city_name\":\"$data->city_name\"," .
            "\"community_name\":\"$data->community_name\"," .
            "  }");
        $result = $aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }


    //验证是否是骑手
    public function riderDetect($data){
        $data = json_decode($data);
        $aop = $this->xcxgetPublicContent();
        $request = new AlipayEbppIndustryCommunityDeliveryrecentorderDetect ();
        $request->setBizContent("{" .
            "\"delivery_open_id\":\"$data->delivery_user_id\"," .
            "\"company_code\":\"$data->company_code\"," .
            "\"city_code\":\"$data->city_code\"," .
            "  }");
        $result = $aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

    //小程序换取授权访问令牌
    public function xcxoauthToken($code){
        $aop = $this->xcxgetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ($request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";   
        }
        return $result->$responseNode;
    }


    //换取授权访问令牌
    public function oauthToken($code){
        $aop = $this->lifegetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ( $request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        }
        return $result->$responseNode;
    }


}